//
//  ESPhotoTableViewCell.m
//  EyeshotPhoto
//
//  Created by admin on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESPhotoTableViewCell.h"


@interface ESPhotoTableViewCell ()

@property (nonatomic, strong) ESCellTopView *topView;
//@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *bigView;
@property (nonatomic, assign) CGFloat imageH;


@end


@implementation ESPhotoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
         self.backgroundColor = [UIColor colorWithRed: 234/255.0 green:234/255.0 blue: 234/255.0 alpha:1.0];
        
        self.bigView = [[UIView alloc] init];
        
        self.topView = [ESCellTopView cellTopView];
        
        self.txtLabel = [[UILabel alloc] init];
        
        self.imageV = [[UIImageView alloc] init];
        
        self.bottomView = [[ESToolbar alloc] init];
        
        [self.contentView addSubview:self.bigView];
        [self.bigView addSubview:self.topView];
        [self.bigView addSubview:self.txtLabel];
        [self.bigView addSubview:self.imageV];
        [self.bigView addSubview:self.bottomView];

       
        
    }
    return self;
}

- (void)setPhotoModel:(ESNewPhotoModel *)photoModel
{
    _photoModel = photoModel;
    
    //设置frame
    CGFloat topViewH = 40;
    CGFloat topViewW = kScreenW - 20;
    self.topView.frame = CGRectMake(5, 5, topViewW, topViewH);
    _topView.backgroundColor = [UIColor whiteColor];
    //添加事件，跳转到一个用户的详情页面
    [_topView.userProfile addTarget:self action:@selector(intoUser:) forControlEvents:(UIControlEventTouchUpInside)];
    _topView.userProfile.userid = photoModel.user_id;
    [_topView.userProfile sd_setImageWithURL:[NSURL URLWithString:photoModel.profile_image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default64"]];
    [_topView.userProfile setTitle:photoModel.name forState:UIControlStateNormal];
    _topView.userProfile.subLabel.text = photoModel.passtime;
    
    [_topView.userProfile setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _topView.userProfile.titleLabel.font = [UIFont systemFontOfSize:12];
    _topView.userProfile.titleLabel.textAlignment = NSTextAlignmentLeft;
    _topView.userProfile.subLabel.textColor = [UIColor grayColor];
    _topView.userProfile.subLabel.font = [UIFont systemFontOfSize:10];
    
    
    CGFloat txtY = 5 + topViewH + 10;
    CGFloat txtH = [ESPhotoTableViewCell heightForStr:photoModel.text withFontSize:15];
    self.txtLabel.frame = CGRectMake(5, txtY, kScreenW - 20, txtH);
    
    CGFloat imageh = [[NSString stringWithFormat:@"%@",photoModel.height] floatValue];
    CGFloat imagew = [[NSString stringWithFormat:@"%@",photoModel.width] floatValue];
    CGFloat imageW = kScreenW - 20;
    CGFloat imageH = 0;
//    if (imagew == 0)
//    {
//        imageH = 0;
//    }else
//    {
        imageH = imageh * imageW / imagew;

//    }
   
    CGFloat imageY = txtY + txtH + 10;
    self.imageV.frame = CGRectMake(5, imageY, imageW, imageH);
    
    CGFloat bottomViewY = imageY + imageH + 10;
    CGFloat bottomViewX = 0;
    CGFloat bottomViewW = kScreenW - 10;
    CGFloat bottomViewH = 30;
    self.bottomView.frame = CGRectMake(bottomViewX, bottomViewY, bottomViewW, bottomViewH);
    CGFloat bigViewH = CGRectGetMaxY(self.bottomView.frame);
    self.bigView.frame = CGRectMake(5, 5, kScreenW - 10, bigViewH);
    
    
    //设置控件内容
    self.bigView.backgroundColor = [UIColor whiteColor];
    
    self.txtLabel.text = photoModel.text;
    self.txtLabel.font = [UIFont systemFontOfSize:15];
    self.txtLabel.numberOfLines = 0;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:photoModel.cdn_img] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    
    self.bottomView.backgroundColor = [UIColor whiteColor];
    //判断button中是数字或字体，再次赋值
    [self setButtonWithTitle:photoModel.love type:ESToolbarBtnTypeDing];
    [self setButtonWithTitle:photoModel.hate type:ESToolbarBtnTypeCai];
    [self setButtonWithTitle:photoModel.repost type:ESToolbarBtnTypeShare];
    [self setButtonWithTitle:photoModel.comment type:ESToolbarBtnTypeComment];
    
    //为图片添加点击事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)];
    self.imageV.userInteractionEnabled = YES;
    [self.imageV addGestureRecognizer:tapGesture];
}

//判断进而得出底部的button是该放数字还是字体
- (void)setButtonWithTitle:(id)title type:(ESToolbarBtnType)type
{
    if (![title isEqualToString:@"0"])
    {
        UIButton *btn = (UIButton *)[self.bottomView viewWithTag:type];
        [btn setTitle:title forState:(UIControlStateNormal)];
    }
}

//图片点击事件
- (void)clickImage:(UITapGestureRecognizer *)tap
{
    if (_delegate && [_delegate respondsToSelector:@selector(photoTableViewCell:didSeletedImage:)])
    {
        [_delegate photoTableViewCell:self didSeletedImage:self.imageV.image];
    }
}

//进入用户详情控制器
- (void)intoUser:(ESCellUserProfile *)btn
{
    if ([_delegate respondsToSelector:@selector(photoTableViewCell:didSeletedUserBtn:)])
    {
        [_delegate photoTableViewCell:self didSeletedUserBtn:btn];
    }

    
}



+ (CGFloat)heightForStr:(NSString *)str withFontSize:(NSInteger)size
{
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size] forKey:NSFontAttributeName];
    CGRect bound = [str boundingRectWithSize:CGSizeMake(kScreenW - 20, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    return bound.size.height;
}

+ (CGFloat)heightForImageWithModel:(ESNewPhotoModel *)photoModel {
    CGFloat imageh = [[NSString stringWithFormat:@"%@",photoModel.height] floatValue];
    CGFloat imagew = [[NSString stringWithFormat:@"%@",photoModel.width] floatValue];
    CGFloat imageW = kScreenW - 20;
    CGFloat imageH = 0;
//    if (imagew == 0)
//    {
//        imageH = 0;
//    }else
//    {
        imageH = imageh * imageW / imagew;

//    }

    return imageH;
}

+ (CGFloat)heightForCellWithModel:(ESNewPhotoModel *)model
{
    CGFloat textH = [self heightForStr:model.text withFontSize:15];
    CGFloat imageH = [self heightForImageWithModel:model];
    return (5 + 40 + 10 + textH + 10 + imageH + 10 + 30 + 10);
}





@end
